﻿unit GetMListFrm;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, GridsEh, DBGridEh, JvExControls, JvXPCore, JvXPButtons, ExtCtrls, DB,
  FIBDataSet, pFIBDataSet;

type
  TGetMListForm = class(TForm)
    Panel1: TPanel;
    JvXPButton1: TJvXPButton;
    JvXPButton2: TJvXPButton;
    Grid: TDBGridEh;
    DS: TpFIBDataSet;
    DataSource: TDataSource;
    procedure GridKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
    procedure FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
  private
    fIsEvent: boolean; //признак события
    fShablon: string;  //признак сотрудника группы выезда
    fCodes: string;
    procedure SetCodes(const Value: string); //список допустимых мероприятий
  public
    property IsEvent: boolean read fIsEvent write fIsEvent default false;
    property Shablon: string read fShablon write fShablon;
    property Codes: string read fCodes write SetCodes;
    procedure OpenDataSet;
  end;

var
  GetMListForm: TGetMListForm;

implementation
 uses  cmUtils, DModule;

{$R *.dfm}

procedure TGetMListForm.FormKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
 case Key of
  VK_CANCEL: ModalResult:=mrCancel;
 end;
end;

procedure TGetMListForm.GridKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
 case Key of
  VK_Escape: ModalResult:=mrCancel;
 end;
end;

procedure TGetMListForm.OpenDataSet;
  var S: string;
begin
 DS.Conditions.CancelApply;
 DS.Conditions.Clear;
 if fIsEvent then S:='0' else S:='1';
 DS.Conditions.AddCondition('Owner','IsControl='+S, true);
 if Shablon<>'' then
  DS.Conditions.AddCondition('Events','Owner LIKE '#39+Shablon+#39, true);
 if Codes<>'' then
  DS.Conditions.AddCondition('Codes', 'Code IN ('+Codes+')', true);
 DS.Conditions.Apply; 
 DS.CloseOpen(true);
 S:=DS.SQLs.SelectSQL.Text;
end;

procedure TGetMListForm.SetCodes(const Value: string);
 var i,Count: integer;
     S: string;
begin
 fCodes := '';
 Count:=WordCount(Value,';');
 for i:=1 to Count do
  begin
   S:=GetWord(Value,i,';');
   if S<>'' then
    begin
     if fCodes<>'' then fCodes:=fCodes+',';
     fCodes:=fCodes+#39+S+#39;
    end;
  end;
end;

end.
